package com.dm.shop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dm.shop.common.enums.ResultEnum;
import com.dm.shop.entity.Product;
import com.dm.shop.mapper.ProductMapper;
import com.dm.shop.mapper.ShopCartMapper;
import com.dm.shop.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dm.shop.utils.ResultUtil;
import com.dm.shop.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MyBatisPlusGenerater-Saiya Wang
 * @since 2021-07-20
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {


    @Autowired
    private ProductMapper productMapper;

    @Override
    public Integer addProduct(Product product) {
//        (#{product.name},#{product.shopName},#{product.price},#{product.promotePrice},#{product.stock},#{product.cid},#{product.saleNum},#{product.collectNum})")
        productMapper.insertProduct(product.getName(),product.getStore(),product.getOriginalPrice(),product.getPromotePrice(),product.getStock(),product.getCid(),product.getSaleNum(),product.getCollectNum());

        return product.getId();
    }

    @Override
    public Result editProduct(Product product) {
        boolean IsUpdate=this.updateById(product);
        if(IsUpdate!=true){
            return ResultUtil.error(ResultEnum.FAIL);
        }else {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
    }

    @Override
    public Result getAllProductInfo(int page, int size) {
        IPage<Product> productIPage = this.page(new Page<>(page,size), null);
        if(productIPage!=null){
            return ResultUtil.success(productIPage);
        }else {
            return ResultUtil.error(ResultEnum.SUCCESS);
        }

    }

    @Override
    public Result delProduct(int id) {
        return null;
    }

    @Override
    public Result keySearch(String keyword) {
        List<Product> productList=productMapper.keySearch(keyword);
        if(productList!=null)
        {
            return ResultUtil.success(productList);
        }
        else {
            return ResultUtil.error(ResultEnum.FAIL);
        }

    }
}
